package online.longzipeng.householdbillssystem.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import online.longzipeng.householdbillssystem.business.entity.Bill;
import online.longzipeng.householdbillssystem.business.entity.Group;
import online.longzipeng.householdbillssystem.business.vo.group.GroupSaveVO;
import online.longzipeng.householdbillssystem.business.vo.group.GroupUpdateVO;
import online.longzipeng.householdbillssystem.business.vo.querey.QueryVO;
import online.longzipeng.householdbillssystem.common.result.ResponseEnum;
import online.longzipeng.householdbillssystem.common.result.Result;
import online.longzipeng.householdbillssystem.common.utils.ExceptionUtils;
import online.longzipeng.householdbillssystem.service.GroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-17
 */
@RestController
@RequestMapping("/group")
@Api(value = "用户组相关接口",description = "用户组相关接口",tags = "用户组相关接口")
@Slf4j
public class GroupController {

    @Autowired
    private GroupService groupService;

    @PostMapping("/get-list-page")
    @ApiOperation("查询所有用户组带分页")
    public Result getListPage(@RequestBody QueryVO queryVO){
        IPage<Group> groupPage = groupService.getListPage(queryVO);
        return Result.success(groupPage);
    }

    @PostMapping("/save")
    @ApiOperation("新建保存用户组信息")
    public Result save(@Valid @RequestBody GroupSaveVO groupSaveVO){
        Group group = new Group();
        BeanUtils.copyProperties(groupSaveVO,group);
        ExceptionUtils.isTrue(groupService.add(group),"用户组保存失败！！请检查填写的用户组信息",group);
        if (log.isDebugEnabled()){
            log.info(String.format("新建用户组成功,用户组信息：%s",group.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @DeleteMapping("/delete_one/{id}")
    @ApiOperation("删除单个用户组信息")
    public Result deleteOne(@ApiParam(value="用户组id",required = true) @PathVariable("id") Integer id){
        ExceptionUtils.isTrue(groupService.delete(id),"删除失败！！参数不合法或已被其他用户删除",id);
        if (log.isDebugEnabled()){
            log.info(String.format("用户组删除成功,用户组信息：%d",id));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PostMapping("/delete")
    @ApiOperation("删除多个用户组信息")
    public Result delete(@ApiParam(value="用户组id数组",required = true, example = "1,2,3,4") @RequestParam("ids") Integer[] ids){
        for (Integer id : ids) {
            ExceptionUtils.isTrue(groupService.delete(id),"删除失败！！参数不合法或已被其他用户删除",ids);
        }
        if (log.isDebugEnabled()){
            log.info(String.format("用户组批量删除成功，ids：%s",ids.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PutMapping("/update")
    @ApiOperation("修改用户组信息")
    public Result update(@Valid @RequestBody GroupUpdateVO groupUpdateVO){
        Group group = new Group();
        BeanUtils.copyProperties(groupUpdateVO,group);
        ExceptionUtils.isTrue(groupService.edit(group),"更新失败！！请检查输入的数据",group);
        if (log.isDebugEnabled()){
            log.info(String.format("修改用户组信息成功，用户组信息：%s",group.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @GetMapping("/find_by_userId/{id}")
    @ApiOperation("查询用户所在的所有用户组")
    public Result findByUserId(@ApiParam(value = "用户id",required = true) @PathVariable("id") Integer id) {
        List<Group> groups = groupService.findByUserId(id);
        Result result = new Result(ResponseEnum.SUCCESS);
        result.setData(groups);
        return result;
    }

}
